﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Peak.ApplicationServices.Reports;
using Peak.ApplicationServices.Services;
using Peak.Core;
using Peak.Core.DataInterfaces;
using Peak.Web.Controllers.Services;

namespace Peak.Web.Controllers
{
  public class PrintController : BaseController
  {
    private readonly IStoryPDFGenerator _storyPdfGenerator;
    private readonly IStoryService _storyService;

    public PrintController(ISessionService sessionService, IStoryPDFGenerator storyPdfGenerator, IStoryService storyService)
      :base(sessionService)
    {
      _storyPdfGenerator = storyPdfGenerator;
      _storyService = storyService;
    }

    public ActionResult Stories(int[] ids)
    {
      List<Story> list = new List<Story>();

      foreach (int id in ids)
      {
        Story story = _storyService.Get(id);
        list.Add(story);        
      }
      byte[] bytes = _storyPdfGenerator.Render(list.ToArray(), _storyPdfGenerator.ReportPath);
      return new FileStreamResult(new MemoryStream(bytes), "application/pdf");
    }

    public ActionResult Story(int id)
    {
      Story story = _storyService.Get(id);      
      byte[] bytes = _storyPdfGenerator.Render(new Story[]{story}, _storyPdfGenerator.ReportPath);
      return new FileStreamResult(new MemoryStream(bytes), "application/pdf");
    }
  }
}
